﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;

namespace Brain
{
#if XBOX
    public class Xbox360InputManager : IInputManager
    {
        public GamePadState[] LastGamePadStates;
        public GamePadState[] GamePadStates;

        public bool ShouldUpdate { get; set; }

        public bool EnableMouseAsTouch = false;

        public Xbox360InputManager()
        {
            GamePadStates = new GamePadState[4];
            LastGamePadStates = new GamePadState[4];
            for (int i = 0; i < 4; i++)
            {
                GamePadStates[i] = GamePad.GetState((PlayerIndex)i);
                LastGamePadStates[i] = GamePadStates[i];
            }

            ShouldUpdate = true;
        }

        #region Exclusive Windows

        public bool IsKeyDown(Enum Key)
        {
            return false;
        }

        public bool IsKeyUp(Enum Key)
        {
            return false;
        }

        public bool IsKeyPress(Enum Key)
        {
            return false;
        }

        #region Mouse

        public Vector2 GetMouseDifference()
        {
            return Vector2.Zero;
        }

        public void SetMousePosition(int X, int Y)
        {
            
        }

        public Vector2 GetMousePosition()
        {
            return Vector2.Zero;
        }

        public bool IsMouseLeftDown()
        {
            return false;
        }
        public bool IsMouseRightDown()
        {
            return false;
        }
        public bool IsMouseMiddleDown()
        {
            return false;
        }

        public bool IsMouseLeftUp()
        {
            return false;
        }
        public bool IsMouseRightUp()
        {
            return false; 
        }
        public bool IsMouseMiddleUp()
        {
            return false;
        }


        public bool IsMouseLeftClick()
        {
            return false;
        }
        public bool IsMouseRightClick()
        {
            return false;
        }
        public bool IsMouseMiddleClick()
        {
            return false;
        }

        #endregion

        #endregion


        #region Xbox and Win

        public float GetLeftTrigger(PlayerIndex PlayerIndex)
        {
            return GamePadStates[(int)PlayerIndex].Triggers.Left;
        }

        public float GetRightTrigger(PlayerIndex PlayerIndex)
        {
            return GamePadStates[(int)PlayerIndex].Triggers.Right;
        }

        public Vector2 GetLeftThumbstick(PlayerIndex PlayerIndex)
        {
            return GamePadStates[(int)PlayerIndex].ThumbSticks.Left;
        }

        public Vector2 GetRightThumbstick(PlayerIndex PlayerIndex)
        {
            return GamePadStates[(int)PlayerIndex].ThumbSticks.Right;
        }

        public bool IsButtonDown(Enum Button, PlayerIndex PlayerIndex)
        {
            return GamePadStates[(int)PlayerIndex].IsButtonDown((Buttons)Button);
        }

        public bool IsButtonUp(Enum Button, PlayerIndex PlayerIndex)
        {
            return GamePadStates[(int)PlayerIndex].IsButtonUp((Buttons)Button);
        }

        public bool IsButtonPress(Enum Button, PlayerIndex PlayerIndex)
        {
            return GamePadStates[(int)PlayerIndex].IsButtonDown((Buttons)Button) &&
                LastGamePadStates[(int)PlayerIndex].IsButtonUp((Buttons)Button);
        }

        public bool IsAnyPlayerButtonDown(Enum Button)
        {
            for (int i = 0; i < 4; i++)
            {
                if (IsButtonDown(Button, (PlayerIndex)i))
                {
                    return true;
                }
            }
            return false;
        }

        public bool IsAnyPlayerButtonUp(Enum Button)
        {
            for (int i = 0; i < 4; i++)
            {
                if (IsButtonUp(Button, (PlayerIndex)i))
                {
                    return true;
                }
            }
            return false;
        }

        public bool IsAnyPlayerButtonPress(Enum Button)
        {
            for (int i = 0; i < 4; i++)
            {
                if (IsButtonPress(Button, (PlayerIndex)i))
                {
                    return true;
                }
            }
            return false;
        }

        #endregion


        #region Windows Phone

        public Vector2[] GetTouchLocations()
        {
            if (EnableMouseAsTouch)
            {
                return new Vector2[] { GetMousePosition() };
            }
            else
            {
                throw new NotImplementedException();
            }
        }

        #endregion

        public void Reset()
        {
            GamePadStates = new GamePadState[4];
            LastGamePadStates = new GamePadState[4];

            for (int i = 0; i < 4; i++)
            {
                LastGamePadStates[i] = GamePadStates[i];
                GamePadStates[i] = GamePad.GetState((PlayerIndex)i);
            }

            ShouldUpdate = true;
        }


        public void Update()
        {
            for (int i = 0; i < 4; i++)
            {
                LastGamePadStates[i] = GamePadStates[i];
                GamePadStates[i] = GamePad.GetState((PlayerIndex)i);
            }
        }

        public SerializationData Data { get; set; }

        public void SerializeData(DefaultSerializationManager manager)
        {
        }

        public void DeserializeData(DefaultSerializationManager manager, SerializationData newData)
        {
        }



    }
#endif
}
